package com.dwxt.cdms.service;

import com.dwxt.cdms.dao.HealthWarnRecordMapper;
import com.dwxt.cdms.dao.WarningMapper;
import com.dwxt.cdms.util.DictUtil;
import com.dwxt.cdms.view.WarningView;
import com.dwxt.cdms.vo.SelectWaringVo;
import com.dwxt.common.base.BaseResult;
import com.dwxt.cdms.base.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ZtService {

    private final WarningMapper warningMapper;
    private final HealthWarnRecordMapper healthWarnRecordMapper;


    public BaseResult selectWarningList(Integer type, Page page) {
        List<Map> list = new ArrayList<>();
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        if (type == 1) {//围栏
            SelectWaringVo vo = new SelectWaringVo();
            vo.setType("1");
            List<WarningView> warningViews = warningMapper.selectWarningByCondition(vo);
            list = WarningList2MapList(warningViews);
        } else if (type ==2){//SOS
            SelectWaringVo vo = new SelectWaringVo();
            vo.setType("2");
            List<WarningView> warningViews = warningMapper.selectWarningByCondition(vo);
            list = WarningList2MapList(warningViews);
        }else if (type ==3){//SOS取消
            SelectWaringVo vo = new SelectWaringVo();
            vo.setType("56");
            List<WarningView> warningViews = warningMapper.selectWarningByCondition(vo);
            list = WarningList2MapList(warningViews);
        } else if (type ==4){//低电量
            SelectWaringVo vo = new SelectWaringVo();
            vo.setType("18");
            List<WarningView> warningViews = warningMapper.selectWarningByCondition(vo);
            list = WarningList2MapList(warningViews);
        }else if (type ==5){//关机
            SelectWaringVo vo = new SelectWaringVo();
            vo.setType("20");
            List<WarningView> warningViews = warningMapper.selectWarningByCondition(vo);
            list = WarningList2MapList(warningViews);
        }else if (type ==6){//跌倒
            SelectWaringVo vo = new SelectWaringVo();
            vo.setType("60");
            List<WarningView> warningViews = warningMapper.selectWarningByCondition(vo);
            list = WarningList2MapList(warningViews);
        }else if (type ==7){//摘除
            SelectWaringVo vo = new SelectWaringVo();
            vo.setType("60");
            List<WarningView> warningViews = warningMapper.selectWarningByCondition(vo);
            list = WarningList2MapList(warningViews);
        }else if (type ==8){//摘除24小时
            List<WarningView> warningViews = warningMapper.selectRemoveWarning(24);
            list = WarningList2MapList(warningViews);
        }else if (type ==9){//摘除72小时
            List<WarningView> warningViews = warningMapper.selectRemoveWarning(72);
            list = WarningList2MapList(warningViews);
        }else if (type ==10){
            list = healthWarnRecordMapper.selectHealthRecordPage(null, null, null, null,null,"体温",null,null);
        }else if (type ==11){
            list = healthWarnRecordMapper.selectHealthRecordPage(null, null, null, null,null,"尿酸",null,null);
        }else if (type ==12){
            list = healthWarnRecordMapper.selectHealthRecordPage(null, null, null, null,null,"脉搏",null,null);
        }else if (type ==13){
            list = healthWarnRecordMapper.selectHealthRecordPage(null, null, null, null,null,"腕温",null,null);
        }else if (type ==14){
            list = healthWarnRecordMapper.selectHealthRecordPage(null, null, null, null,null,"血压",null,null);
        }else if (type ==15){
            list = healthWarnRecordMapper.selectHealthRecordPage(null, null, null, null,null,"血氧",null,null);
        }else if (type ==16){
            list = healthWarnRecordMapper.selectHealthRecordPage(null, null, null, null,null,"血糖",null,null);
        }else if (type ==17){
            list = healthWarnRecordMapper.selectHealthRecordPage(null, null, null, null,null,"血酮",null,null);
        }
        PageInfo<Map> pageInfo = new PageInfo<>(list);
        if (type >= 10) {
            List<Map> maps = handleDeviceParam(list);
            pageInfo.setList(maps);
        }
        return BaseResult.ok(pageInfo);
    }


    private List<Map> handleDeviceParam(List<Map> list) {
        List<Map> collect = list.stream().map(map1 -> {
            //map1.put("phone", map1.get("phone").toString().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
            Object device_type = map1.get("device_type");
            if (device_type != null) {
                String deviceType = device_type.toString();
                String deviceDisplay = DictUtil.getDisplay("category", deviceType);
                map1.put("deviceDisplay", deviceDisplay);
            }
            return map1;
        }).collect(Collectors.toList());
        return list;
    }

    private List<Map> WarningList2MapList(List<WarningView> list) {
        ArrayList<Map> mapList = new ArrayList<>();
        for (WarningView warn: list) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("age", warn.getAge());
            map.put("category", warn.getTypeDisplay());
            map.put("check_time", warn.getWarningTime());
            map.put("create_time", warn.getCreateTime());
            map.put("detail", warn.getContent());
            map.put("deviceDisplay", warn.getDeviceDisplay());
            map.put("device_type", warn.getCategory());
            map.put("id", warn.getId());
            map.put("is_handle", warn.getState());
            map.put("name", warn.getName());
            map.put("phone", warn.getPhone());
            map.put("sex", warn.getSex());
            map.put("user_id", warn.getCustomerId());
            mapList.add(map);
        }
        return mapList;
    }
}
